<!--
 * @Description: 
 * @Author: xuzp4
-->
<template>
  <el-dialog
    class="toast-dialog"
    :visible="_visible"
    :close-on-click-modal="false"
    :title="
      type === 'add' ? $t('yonghuguanli.addRole') : $t('yonghuguanli.editRole')
    "
    :append-to-body="true"
    width="520px"
    :before-close="handleClose"
  >
    <div class="content d-flex jc-center ai-center">
      <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
        <el-form-item :label="$t('yonghuguanli.role_name')" prop="group_name">
          <el-input :disabled="type === 'edit'" v-model="formData.group_name" />
        </el-form-item>
        <el-form-item
          :label="$t('yonghuguanli.permission') + ':'"
          prop="permission"
        >
          <ResourceTree
            :visible="_visible"
            ref="treeWrapper"
            :disabled="false"
            :permission="formData.permission"
            @updatePermission="updatePermission"
          />
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <MyButton class="cancel" @click="cancel">{{
        $t("common.cancel")
      }}</MyButton>
      <MyButton class="confirm" @click="confirm">{{
        $t("common.confirm")
      }}</MyButton>
    </div>
  </el-dialog>
</template>

<script>
import { addRole, updateRole } from "./service";
import ResourceTree from "./Resource.vue";
import util from "@/utils/index.js";
export default {
  components: { ResourceTree },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    row: {
      type: Object,
      default: () => {
        return {};
      },
    },
    type: {
      type: String,
      default: "add",
    },
  },
  watch: {
    visible(val) {
      if (val) {
        this.$nextTick(() => {
          this.$refs.form.clearValidate();
        });
      }
    },
    row: {
      handler(val) {
        if (val) {
          this.$set(this, "formData", {
            group_id: val.group_id,
            group_name: val.group_name,
            permission: val.permission,
          });
        }
      },
      deep: true,
      immediate: true,
    },
  },
  computed: {
    _visible: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit("update:visible", val);
      },
    },
  },
  data() {
    return {
      formData: {},
      rules: {},
      selectedPermission: "",
    };
  },
  methods: {
    updatePermission(resourceData, val) {
      this.selectedPermission = resourceData.reduce((cur, next) => {
        if (!val.includes(next.authIndex)) {
          cur =
            cur.slice(0, next.authIndex) + "0" + cur.slice(next.authIndex + 1);
        }
        return cur;
      }, "1".repeat(22));
    },
    confirm() {
      if (this.type === "add") {
        this.$refs.form.validate((valid) => {
          if (valid) {
            const params = {
              ...this.formData,
              group_id: 0,
              permission: this.selectedPermission,
            };
            addRole(params).then((res) => {
              if (res.code === 0) {
                util.msg.success("add_success");
                this._visible = false;
                this.$emit("getList");
              }
            });
          }
        });
      } else {
        this.$refs.form.validate((valid) => {
          if (valid) {
            const params = {
              ...this.formData,
              permission: this.selectedPermission,
            };
            updateRole(params).then((res) => {
              if (res.code === 0) {
                util.msg.success("edit_success");
                this._visible = false;
                this.$emit("getList");
              }
            });
          }
        });
      }
    },
    cancel() {
      this._visible = false;
      this.$refs.form.resetFields();
    },
    handleClose(done) {
      this.$refs.form.resetFields();
      this._visible = false;
      done();
    },
  },
};
</script>
<style lang="less" scoped>
.toast-dialog {
  z-index: 9999;
  .content {
    width: 100%;
    /deep/ .el-form {
      width: 100%;
    }
    .pwd-box {
      position: relative;
      width: 300px;
      .eye {
        position: absolute;
        right: 10px;
        top: 50%;
        transform: translateY(-50%);
        width: 20px;
        height: 10px;
        background: url("../../../assets/eye.png") no-repeat;
        background-size: 100% 100%;
        cursor: pointer;
      }
    }
  }
}
</style>
